//
//  WmcBrandContentView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/16.
//

import UIKit

class WmcBrandContentView: WmcBaseView {
    /// 水印视图
    weak var watermarkView: WmcBaseWatermarkTemplateView?

    /// 品牌图
    var brandView: WmcBaseBrandView?

    /// 品牌图配置
    var optionBrand: WmcWatermarkOptionModel?

    /// 二级仿射变换
    var secondaryTransform: CGAffineTransform? {
        didSet { uploadBrand() }
    }
    
    /// 点击品牌图时候，需要触发的Block
    var brandHitBrandCallBlock: YPBlock.Return<UIView?>?

    override func layoutSubviews() {
        super.layoutSubviews()
        updateBrandViewFrame()
    }

    override func makeUI() {
        backgroundColor = nil
    }
    
    func uploadBrand() {
        guard let optionBrand = optionBrand else { return }
        configBrand(optionBrand)
    }

    /// 配置品牌图
    func configBrand(_ optionBrand: WmcWatermarkOptionModel) {
        // 先移除之前品牌图
        removeBrand()
        // 创建添加品牌图
        brandView = .CreateBrandTemplateView(optionBrand)
        self.optionBrand = optionBrand
        guard let brandView = brandView else { return }
        brandView.configOptionBrand(optionBrand)
        addSubview(brandView)
        // 点击事件转移
        brandView.brandHitBrandCallBlock = brandHitBrandCallBlock
        
        if let secondaryTransform = secondaryTransform {
            brandView.transform = brandView.transform.concatenating(secondaryTransform)
        }
    }

    /// 移除品牌图
    func removeBrand() {
        brandView?.removeFromSuperview()
        brandView = nil
    }

    /// 当水印移动时
    func watermarkViewMove(_ watermarkView: WmcBaseWatermarkTemplateView) {
        updateBrandViewFrame()
    }

    /// 更新品牌图位置
    func updateBrandViewFrame() {
        guard let optionBrand = optionBrand,
              let brandView = brandView,
              let watermarkView = watermarkView else { return }
        layoutIfNeeded()
        var brandSize = brandView.frame.size
        if brandSize == .zero {
            brandSize = 150.yp.cgSize
        }
        
        switch optionBrand.brand.locationType {
        case .topLeft:
            brandView.frame = CGRect(origin: .zero, size: brandSize)
        case .topRight:
            brandView.frame = CGRect(origin: .init(x: frame.size.width - brandSize.width,
                                                   y: 0),
                                     size: brandSize)
        case .center:
            brandView.frame = CGRect(origin: center, size: brandSize)
            brandView.center = center
        case .bottomLeft:
            brandView.frame = CGRect(origin: .init(x: 0,
                                                   y: frame.size.height - brandSize.height),
                                     size: brandSize)
        case .bottomRight:
            brandView.frame = CGRect(origin: .init(x: frame.size.width - brandSize.width,
                                                   y: frame.size.height - brandSize.height),
                                     size: brandSize)
        case .followWatermark:
            brandView.frame = CGRect(origin: .init(x: watermarkView.frame.origin.x + 8,
                                                   y: watermarkView.frame.origin.y - brandView.frame.height),
                                     size: brandView.frame.size)
            // 跟随水印时设置边距
            var topOffset: CGFloat = 0
            if brandView.frame.size.height > 0 {
                topOffset = 8
            }
            watermarkView.edgeCompensateDistance.top = brandView.frame.size.height + topOffset
            var rightEdgeCompensateDistance: CGFloat = 0
            let brandViewSpaceWidth = brandView.frame.size.width + 16
            if watermarkView.frame.size.width < brandViewSpaceWidth {
                rightEdgeCompensateDistance = brandViewSpaceWidth - watermarkView.frame.size.width
            }
            watermarkView.edgeCompensateDistance.right = rightEdgeCompensateDistance
        }
    }
}
